#!/usr/bin/env python
# Standard imports
from __future__ import print_function
import os
# Third-Party imports
import pygame
from OpenGL.GL import *
import Image
# Custom imports
import Options

glEnable(GL_TEXTURE_2D)

class Texture():
    def __init__(self, FileName):
        self.ID = 0
        self.FileName = ""
        self.FileLocation = ""
        self.Image = ""
        self.FilterStyle = Options.TextureFilterStyle
        self.WrapStyle = Options.TextureWrapStyle
        self.Type = GL_TEXTURE_2D
        self.Width = Options.TextureWidth
        self.Height = Options.TextureHeight
        self.Border = Options.TextureBorder
        self.Depth = Options.TextureDepth
        self.TextureEnvMode = Options.TextureEnvMode
        self.LoadImage(FileName)

    def LoadImage(self, FileName):
        print("Loading "+str(FileName))
        # File IO
        self.FileName = FileName
        self.FileLocation = os.path.join(Options.TexturesLocation, FileName)
        image = Image.open(self.FileLocation)
        try:
            # get image meta-data (dimensions) and data
            self.Width, self.Height, self.Image = image.size[0], image.size[1], image.tostring("raw", "RGBA", 0, -1)
        except SystemError:
            # has no alpha channel, synthesize one, see the
            # texture module for more realistic handling
            self.Width, self.Height, self.Image = image.size[0], image.size[1], image.tostring("raw", "RGBX", 0, -1)
        del image

        # GL Settings
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
        self.ID = glGenTextures(1)
        print(self.ID)
        self.Bind()
        glTexImage2D(self.Type, 0, GL_RGBA, self.Width, self.Height,
                     self.Border, GL_BGR, GL_UNSIGNED_BYTE, self.Image)

        glTexParameterf(self.Type, GL_TEXTURE_WRAP_T, self.WrapStyle)
        glTexParameterf(self.Type, GL_TEXTURE_WRAP_S, self.WrapStyle)
        glTexParameterf(self.Type, GL_TEXTURE_MAG_FILTER, self.FilterStyle)
        glTexParameterf(self.Type, GL_TEXTURE_MIN_FILTER, self.FilterStyle)
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, self.TextureEnvMode)


    def IsBound(self):
        """Checks to see if our type matches the currently bound texture of the same type"""
        if glGetBoolean(GL_TEXTURE_BINDING_2D) == self.ID: return True
        else: return False

    def IsValid(self):
        """Checks if we're a valid texture"""
        result = glIsTexture(self.ID)
        print("!"+str(result))
        if result == GL_TRUE:
            return True
        else:
            return False

    def Bind(self):
        """Binds our texture to the placeholder for our texture's type.
        Think of it like putting a color of paint on your paintbrush.
        We only have one paintbrush for each texture type,
        (1D, 2D, 3D, or Cube Map), it's quicker to bind to an already loaded
        texture than it is to load to the placeholder on demand."""
        glBindTexture(self.Type, self.ID)
        #glClientActiveTexture(self.ID)

    def UnBind(self):
        """Restores the default texture of our type to the binding for our type"""
        glBindTexture(self.Type, 0)
        #glClientActiveTexture(self.ID)